const n = require("../../utils/request"); 
o = require("../../utils/storage"),
e = require("../../utils/config");
Page({
    data: {
     showSign:false,
      form:{
        reserve_time:'',
        typename:'',
        unit_name:'',
        name:'',
        gendertext:'',
        age:'',
        phone:'',
        identity_number:'',
        examine_typetext:'',
      },
      genderLabels: ['男', '女'],
      genderValues: [1, 2], // 对应的性别值，1表示男，2表示女
      selectedGenderIndex: 0,
      typeLabels: ['单位', '个人'],
      typeValues: [1, 2], // 对应的性别值，1表示单位，2表示个人
      selectedTypeIndex: 0, 
      selectedExamineIndex: 0,
      identityNumber:0,
      phoneNumber:0,
      startDate:"2024-01-01",
      date:"2024-01-01",
      dateText:"2024-01-01",
      timeValue:[],
      timeIndex:0,
      time:"00:00",
      zid:""
    },
     
    onLoad: function(option) {
      let that = this;  
      console.log(option)
      if(option.zid){
        this.zid=option.zid
      }
      //初始化时间
      this.getCarddetail();
    },
    onShow: function(option) {    
      let that = this;     
  },
  cancelCanvas:function() {
    this.setData({showSign:false})
  },
  viewSign:function() {
    this.setData({showSign:true})
  },
  saveToImageEvent:(e) => {
    wx.getFileSystemManager().readFile({
        filePath: e.detail,  //图片路径
        encoding: 'base64', //编码格式
        success: res2 => { //成功的回调
          console.log('data:image/png;base64,' + res2.data)
        },
        fail:(err)=>{
            console.log('err',err)
        }
      })
  },
    getCarddetail: function(t) {
      var openid = storageData.getStorageSync(storageData.storageKey.openId);
      var that = this;
      let para={'openid':openid,'zid':that.zid}               
      n.post1("/wechat/home/getTjinfo", para, !0)
      .then(function(i) { 
          if(i.code == 1){ 
              console.log(i.data) 
              let data1=i.data;
              that.setData({
               form:i.data
              });
          }else (0, e.default)({
              message: i.msg,
              duration: 5e3
          }), wx.hideLoading();
          
      });
  },

    bindDateChange(e){
      let that = this;
      let date = e.detail.value;
      let timeStamp1 = Date.parse(new Date(date));
      let timeStamp2 = Date.parse(new Date(this.data.startDate));
      if(timeStamp1>timeStamp2){
        let timeValue = [];
        for(let i=0;i<24 ;i++){
          timeValue.push(i);
        }
        that.setData({
          timeValue:timeValue
        })
      }
      console.log(date)
      that.setData({
        dete:date,
        dateText:date,
        reserveTime: date +" "+that.data.time+":00"
      });
    },
    bindTimeChange(e){
      let that = this;
      let timeIndex = e.detail.value;
      let hours = that.data.timeValue[timeIndex];
      that.setData({
        timeIndex:timeIndex,
        time:String(hours).padStart(2, '0')+":00",
        reserveTime: that.data.date +" "+String(hours).padStart(2, '0')+":00"
      });
    },
    bindPickerChangeType(e){
      this.setData({
        selectedTypeIndex: e.detail.value
      });
    },
    bindPickerChangeGender: function(e) {
      this.setData({
        selectedGenderIndex: e.detail.value
      });
    },
    bindPickerChangeExamine: function(e) {
      this.setData({
        selectedExamineIndex: e.detail.value
      });
    },
    bindInputIdentityNumber: function(e) {
      this.setData({
        identityNumber: e.detail.value
      });
    },
    bindInputPhoneNumber: function(e) {
      this.setData({
        phoneNumber: e.detail.value
      });
    },
    formSubmit(e) {
      console.log(this.data.reserveTime);
      if (!this.validateIdentityNumber(this.data.identityNumber)) {
        wx.showToast({
          title: '身份证号码不合法',
          icon: 'none'
        });
        return;
      }
      if(!this.validatePhoneNumber(this.data.phoneNumber)){
        wx.showToast({
          title: '手机号不合法不合法',
          icon: 'none'
        });
        return;
      }
    
      if(e.detail.value.type==1 &&  !e.detail.value.unit_name){
        wx.showToast({
          title: '单位名称不能为空',
          icon: 'none'
        });
        return;
      }
      //       var t = r.getStorageSync(r.storageKey.openId);
        e.detail.value.openid=o.getStorageSync(o.storageKey.openId);
        n.post1('wechat/home/addInformation', e.detail.value).then(function(e) {
            if(e.code ==0){
                //添加预约成功
                wx.redirectTo({
                    url: "/pages/myhoscheck/myhoscheck"
                })
            }else {
                wx.showToast({
                    title:e.msg,
                    icon:'none',
                    duration:2000
                })
            }
        })
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
    },
    validateIdentityNumber: function(identityNumber) {
      const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])\d{3}(\d|X)$/;
      return idPattern.test(identityNumber);
    },
    validatePhoneNumber:function(phoneNumber) {
      const regex = /^1[3-9]\d{9}$/;
      console.log(phoneNumber);
      return regex.test(phoneNumber);
    }
});